#include "item.h"
#include <QtXml/QDomDocument>
#include <QtSvg/QSvgRenderer>
#include <QtGui/QPainter>
#include "dactor.h"
#include "drouter.h"
#include "dpool.h"
#include "dcomposite.h"
#include "DElementDelegate.h"

extern QString gActorDir;
extern QString gRouterDir;
extern QString gPoolDir;
extern QString gCompositeDir;
extern QString gElementDir;

void Item::appendChild(Item *item)
{
	item->parentItem = this;
	childItems.append(item);
}

bool ListItem::load( const QDomElement& e)
{
	mDisplayName = e.attribute("displayName");
	QString decorate = e.attribute("decorate");
	if (decorate == QString())  
		decorate = ":/elements/list.svg";
	else 
		decorate = gElementDir+"/"+decorate;
	mToolTip = e.attribute("toolTip");
	mStatusTip = e.attribute("statusTip");
	mWhatsThis = e.attribute("whatsThis");

	//pixmap.fill(Qt::white);
	QPainter p(&pixmap);
	QSvgRenderer svgRender(decorate);
	svgRender.render(&p);
	return true;
}

bool AdapterDElement::load(const QDomElement& e)
{
	QString file = e.attribute("file");
	QString dir;
	if (e.nodeName() == kActor) {
		de = new DActor();
		dir = gActorDir;
	}
	else if (e.nodeName() == kRouter) {
		de = new DRouter();
		dir = gRouterDir;
	}
	else if (e.nodeName() == kPool) { 
		de = new DPool();
		dir = gPoolDir;
	}
	else if (e.nodeName() == kComposite) {
		de = new DComposite();
		dir = gCompositeDir;
	}
	
	if (!de->parseContent(dir+"/"+file)) return false;

	//register this DElement in DElementDelegate
	DElementDelegate::instance()->registerDElement(de);

	QSvgRenderer* renderer = de->renderer();
	if (!renderer) return false;	
	QPainter p(&pixmap);
	renderer->render(&p);

	return true;
}